package com.joint.web.action.com;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.fz.us.base.util.LogUtil;
import com.google.common.collect.Sets;
import com.joint.base.entity.*;
import com.joint.base.service.*;
import com.joint.base.util.DataUtil;
import com.joint.base.util.EntityUtil;
import com.joint.web.action.BaseFlowAction;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.*;


@ParentPackage("com")
public class AjaxStepconAction extends BaseFlowAction {

    @Resource
    private ProcessBriefService processBriefService;
    @Resource
    private StepBriefService stepBriefService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private RoleService roleService;
    @Resource
    private PowerService powerService;
    @Resource
    private PostService postService;
    @Resource
    private CommonConfigService commonConfigService;
    @Resource
    private StepConService stepConService;
    /**
     * 流程简要表(读)
     */
    private ProcessBrief processBrief;
    /**
     * 步骤简要表(读)
     */
    private StepBrief stepBrief;
    /**
     * 步骤条件表(读)
     */
    private StepCon stepCon;

    private String stepName;
    private String content;
    private String formulaField;
    private String formulaFlag;
    private String formulaVal;
    private int stepNo;
    private String[] fieldNames;

    /**
     * 项目视图
     * @return
     */
    public String execute(){
        return "stepcon";
    }



    public String list(){
        pager = new Pager();
        if (rows <= 0){
            rows = 10;
        }
        pager.setPageSize(rows);
        pager.setPageNumber(page);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.asc);
        Company com = usersService.getCompanyByUser();
        Users user = usersService.getLoginInfo();
        Map<String,Object> params = new HashMap<String,Object>();
        //内嵌视图这里要加上
        if (StringUtils.isNotEmpty(parentId)){
            stepBrief = stepBriefService.get(parentId);
            params.put("stepBrief",stepBrief);
        }else{
            return  ajaxJson("{\"total\":0,\"dataRows\":[],\"page\":1,\"records\":0,\"rows\":10}");
        }
        params.put("company",com);
        params.put("state",BaseEnum.StateEnum.Enable);

        pager = stepConService.findByPager(pager, params);

        List<StepCon>  stepConList;
        if (pager.getTotalCount() > 0){
            stepConList = (List<StepCon>) pager.getList();
        }else{
            stepConList = new ArrayList<>();
        }

        List<JSONObject> dataRows=new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rMap;

        for(StepCon stepCon: stepConList){
            rMap = new HashMap<String, Object>();
            rMap.put("id",stepCon.getId());
            rMap.put("content", stepCon.getContent());
            rMap.put("name", stepCon.getName());
            rMap.put("creater", stepCon.getCreater() != null ? stepCon.getCreater().getName() : "");
            rMap.put("createDate", DataUtil.DateToString(stepCon.getCreateDate(), "yyyy-MM-dd"));
            rMap.put("formula", stepCon.getFormulaField()+stepCon.getFormulaFlag()+stepCon.getFormulaVal());
            rMap.put("stepNo", stepCon.getStepNo());
            JSONObject o = JSONObject.fromObject(rMap);
            dataRows.add(o);
        }
        data.put("dataRows",dataRows);
        data.put("page",page);
        data.put("rows",rows);
        long recordsNum = pager.getTotalCount();
        data.put("total",calcPageNum(recordsNum));
        data.put("records", recordsNum);
        return  ajaxJson(JSONObject.fromObject(data).toString());
    }

    public String input(){
        if (StringUtils.isNotEmpty(parentId)){
            stepBrief = stepBriefService.get(parentId);
            stepCon = new StepCon();
            stepCon.setStepBrief(stepBrief);
        }
        if (StringUtils.isNotEmpty(keyId)){
            stepCon = stepConService.get(keyId);
            stepBrief = stepCon.getStepBrief();
        }
        try {
            Class table =  Class.forName(stepBrief.getProcessBrief().getTableKey());
            fieldNames =  EntityUtil.getFiledName(table);
           // LogUtil.info("fieldNames:"+ fieldNames);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return "input";
    }

    /**
     * 步骤简要表保存
     * @return
     */
    public String save(){
        if(StringUtils.isNotEmpty(keyId)){
            stepCon = stepConService.get(keyId);
        }else{
            stepCon = new StepCon();
            stepCon.setCreater(usersService.getLoginInfo());
        }
        if(StringUtils.isNotEmpty(parentId)){
            stepBrief = stepBriefService.get(parentId);
            stepCon.setStepBrief(stepBrief);
        }
        stepCon.setName(stepName);
        stepCon.setContent(content);
        stepCon.setFormulaField(formulaField);
        stepCon.setFormulaFlag(formulaFlag);
        stepCon.setFormulaVal(formulaVal);
        stepCon.setStepNo(stepNo);
        stepCon.setCompany(usersService.getCompanyByUser());
        if(StringUtils.isNotEmpty(keyId)){
            stepConService.update(stepCon);
        }else{
            stepConService.save(stepCon);
        }
        return ajaxHtmlCallback("200", "保存成功！", "操作状态");
    }

    //删除
    public String delete(){
        if(StringUtils.isNotEmpty(keyId)){
            stepCon = stepConService.get(keyId);
            stepCon.setState(BaseEnum.StateEnum.Delete);
            stepConService.update(stepCon);
        }
        return  ajaxHtmlCallback("200", "删除成功！","操作状态");
    }
    /**
     * 分割字符串
     *
     * @param str
     * @return
     */
    private Set<Users> getUserSpit(String str) {
        Set<Users> usersSet = new HashSet<Users>();
        String[] ary = str.split(",");
        for (String item : ary) {
            if (com.joint.base.util.StringUtils.isNotEmpty(item)) {
                usersSet.add(usersService.get(item));
            }

        }
        return usersSet;
    }

    private Set<Department> getDepartSpit(String str) {
        Set<Department> departSet = new HashSet<Department>();
        if(com.joint.base.util.StringUtils.isEmpty(str)) return departSet;
        String[] ary = str.split(",");
        for (String item : ary) {
            if (com.joint.base.util.StringUtils.isNotEmpty(item)) {
                departSet.add(departmentService.get(item));
            }
        }
        return departSet;
    }

    private Set<Post> getPostSpit(String str) {
        Set<Post> postSet = new HashSet<Post>();
        if(com.joint.base.util.StringUtils.isEmpty(str)) return postSet;
        String[] ary = str.split(",");
        for (String item : ary) {
            if (com.joint.base.util.StringUtils.isNotEmpty(item)) {
                postSet.add(postService.get(item));
            }
        }
        return postSet;
    }

    private Set<Role> getRoleSpit(String str) {
        Set<Role> roleSet = Sets.newHashSet();
        if(com.joint.base.util.StringUtils.isEmpty(str)) return roleSet;
        String[] ary = str.split(",");
        for (String item : ary) {
            if (com.joint.base.util.StringUtils.isNotEmpty(item)) {
                roleSet.add(roleService.get(item));
            }
        }
        return roleSet;
    }

    private Set<Power> getPowerSpit(String str) {
        Set<Power> powerSet = Sets.newHashSet();
        if(com.joint.base.util.StringUtils.isEmpty(str)) return powerSet;
        String[] ary = str.split(",");
        for (String item : ary) {
            if (com.joint.base.util.StringUtils.isNotEmpty(item)) {
                powerSet.add(powerService.get(item));
            }
        }
        return powerSet;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ProcessBrief getProcessBrief() {
        return processBrief;
    }

    public void setProcessBrief(ProcessBrief processBrief) {
        this.processBrief = processBrief;
    }

    public StepBrief getStepBrief() {
        return stepBrief;
    }

    public void setStepBrief(StepBrief stepBrief) {
        this.stepBrief = stepBrief;
    }

    public StepCon getStepCon() {
        return stepCon;
    }

    public void setStepCon(StepCon stepCon) {
        this.stepCon = stepCon;
    }

    public String getStepName() {
        return stepName;
    }

    public void setStepName(String stepName) {
        this.stepName = stepName;
    }

    public String getFormulaVal() {
        return formulaVal;
    }

    public void setFormulaVal(String formulaVal) {
        this.formulaVal = formulaVal;
    }

    public String getFormulaField() {
        return formulaField;
    }

    public void setFormulaField(String formulaField) {
        this.formulaField = formulaField;
    }

    public String getFormulaFlag() {
        return formulaFlag;
    }

    public void setFormulaFlag(String formulaFlag) {
        this.formulaFlag = formulaFlag;
    }

    public int getStepNo() {
        return stepNo;
    }

    public void setStepNo(int stepNo) {
        this.stepNo = stepNo;
    }

    public String[] getFieldNames() {
        return fieldNames;
    }

    public void setFieldNames(String[] fieldNames) {
        this.fieldNames = fieldNames;
    }

}

